import request from "@/utils/request";
import { ElMessage } from "element-plus";
import {
  OrderInfo,
  PayInfo,
  Result,
  VisitingUser,
  VisitingUserResult,
} from "@/types/types.ts";
import { UserAuthVo } from "@/types/vo/UserAuthVo.ts";

enum API {
  // 创建订单
  SUBMIT_ORDER = "/order/orderInfo/auth/submitOrder/",
  // 获取订单
  GET_ORDER_INFO = "/order/orderInfo/auth/getOrderInfo/",
  // 取消预约
  CANCEL_ORDER = "/order/orderInfo/auth/cancelOrder/",
  // 下单,获取支付二维码
  CREATE_NATIVE = "/order/weixin/createNative/",
  // 查询支付状态
  PAY_STATUS = "/order/weixin/queryPayStatus/",
  // 获取用户信息
  USER_INFO = "/user/auth/getUserInfo",
  // 用户认证
  USER_AUTH = "/user/auth/userAuah",
  // 获取所有就诊人接口
  ALL_USER = "/user/patient/auth/findAll",
  // 获取就诊人详情
  USER_DETAIL = "/user/patient/auth/get/",
  // 添加或修改就诊人
  USER_ADD = "/user/patient/auth/save",
  USER_UPDATE = "/user/patient/auth/update",
  USER_DEL = "/user/patient/auth/remove/",
}

// 创建订单
export function doSubmitOrder(
  hoscode: string,
  scheduleId: string,
  patientId: string,
): Promise<Result, any> {
  return request({
    url: API.SUBMIT_ORDER + `${hoscode}/${scheduleId}/${patientId}`,
    method: "POST",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
}

// 获取订单
export function doGetOrder(id: number) {
  return request({
    url: API.GET_ORDER_INFO + `${id}`,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
}

// 取消预约
export function doCancelOrder(orderId: number) {
  return request<OrderInfo>({
    url: API.CANCEL_ORDER + `${orderId}`,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
}

// 创建支付二维码
export function doCreateNative(orderId: number) {
  return request<PayInfo>({
    url: API.CREATE_NATIVE + `${orderId}`,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
}

// 查询支付状态
export function doGetPayStatus(orderId: number) {
  return request<Result, any>({
    url: API.PAY_STATUS + `${orderId}`,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
}

// 获取用户信息
export const doGetUserInfo = () => {
  return request<Result, any>({
    url: API.USER_INFO,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
};
// 用户认证
export const doUserAuth = (data: UserAuthVo) => {
  return request<Result, any>({
    url: API.USER_AUTH,
    method: "POST",
    data,
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
};
// 获取当前会员下所有就诊人信息
export const doGetAllUser = () => {
  return request<VisitingUserResult, any>({
    url: API.ALL_USER,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return Promise.reject(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      return Promise.reject(err.message);
    });
};
// 获取就诊人详情
export const doGetUserDetail = (id: number) => {
  return request<VisitingUser, any>({
    url: API.USER_DETAIL + `${id}`,
    method: "GET",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return Promise.reject(res.message);
      }
      return res.data;
    })
    .catch((err) => {
      return Promise.reject(err.message);
    });
};
// 添加就诊人
export const doAddUser = (data: VisitingUser) => {
  return request<Result, any>({
    url: API.USER_ADD,
    method: "POST",
    data,
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return Promise.reject(res.message);
      }
      ElMessage.success("添加成功");
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err.message);
      return Promise.reject(err.message);
    });
};
// 修改就诊人
export const doUpdateUser = (data: VisitingUser) => {
  return request<Result, any>({
    url: API.USER_UPDATE,
    method: "PUT",
    data,
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return Promise.reject(res.message);
      }
      ElMessage.success("修改成功");
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err.message);
      return Promise.reject(err.message);
    });
};
// 删除就诊人
export const doDelUser = (id: number) => {
  return request<Result, any>({
    url: API.USER_DEL + `${id}`,
    method: "DELETE",
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return Promise.reject(res.message);
      }
      ElMessage.success("删除成功");
      return res.data;
    })
    .catch((err) => {
      ElMessage.error(err.message);
      return Promise.reject(err.message);
    });
};
